package com.cqupt.scanning.system.acl.controller;

import com.cqupt.scanning.system.acl.entity.vo.AssignPermissionsVO;
import com.cqupt.scanning.system.acl.service.AclPermissionService;
import com.cqupt.scanning.system.utils.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Data: 2021/7/19 20:59
 * @Author: 宋宝梁
 */
@RestController
@RequestMapping("/admin/acl/permission")
public class AclPermissionController {

    private AclPermissionService aclPermissionService;

    @Autowired
    public AclPermissionController(AclPermissionService aclPermissionService) {
        this.aclPermissionService = aclPermissionService;
    }

    /**
     * 为角色分配权限
     * @param assignPermissionsVO
     * @return
     */
    @PostMapping("/assignPermissions")
    @PreAuthorize("hasAuthority('permission.assignPermissions')")
    public R assignPermissions(@RequestBody @Validated AssignPermissionsVO assignPermissionsVO) {
        return aclPermissionService.assignPermissions(assignPermissionsVO);
    }

    /**
     * 获取所有权限
     * @return
     */
    @GetMapping("/getAllPermissions")
    public R getAllPermissions() {
        return aclPermissionService.getAllPermissions();
    }

    /**
     * 根据角色id获取权限列表
     * @param roleId
     * @return
     */
    @GetMapping("/getPermissionsByRoleId/{id}")
    @PreAuthorize("hasAuthority('user.getPermissionsByRoleId')")
    public R getPermissionsByRoleId(@PathVariable("id") Integer roleId) {
        return aclPermissionService.getPermissionsByRoleId(roleId);
    }



}
